<%--
Copyright (c) 2010 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page
        import="com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource" %>
<%@ page import="com.google.api.client.http.javanet.NetHttpTransport" %>
<%@ page import="com.google.api.client.json.gson.GsonFactory" %>
<%@ page import="com.google.api.services.plus.Plus" %>
<%@ page import="com.google.api.services.plus.model.*" %>
<%@ page import="com.google.api.sample.ConfigHelper" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>ES API Java Samples</title>
    <link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<header><h1>Google+ platform Java Starter App</h1></header>

<div class="largerbox">
    <% if (session.getAttribute("accessToken") == null) { %>
    <a class='login' href='oauth2callback'>Connect Me!</a>
    <% } else { %>
    <p>Your OAuth 2.0 token is in <code>session.accessToken</code>.</p>

    <p class="logout"><a href="clearsession">Logout</a> by deleting this token.
    </p>

    <%
        // Bootstrap a special api client for requests that do not require authentication
        Plus unauthenticatedPlus = new Plus(new NetHttpTransport(), new GsonFactory());
        unauthenticatedPlus.setKey(ConfigHelper.GOOGLE_API_KEY);

        // Bootstrap the api client using our client information
        GoogleAccessProtectedResource requestInitializer = new GoogleAccessProtectedResource(
                session.getAttribute("accessToken").toString(),
                new NetHttpTransport(),
                new GsonFactory(),
                ConfigHelper.CLIENT_ID,
                ConfigHelper.CLIENT_SECRET,
                session.getAttribute("refreshToken").toString());
        Plus plus = new Plus(new NetHttpTransport(), requestInitializer, new GsonFactory());
    %>
    <section class="activity">
        <h3>A Specific Public Activity</h3>
        <%
            // Fetch a specific known public activity
            String publicActivityId = "z12gtjhq3qn2xxl2o224exwiqruvtda0i";
            Activity publicActivity = unauthenticatedPlus
                    .activities().get(publicActivityId).execute();

            // Render it
        %>
        <dl>
            <dt>ID</dt>
            <dd>
                <a href="<%= publicActivity.getUrl() %>"><%= publicActivity.getId() %></a>
            </dd>
            <dt>Contents</dt>
            <dd>
                <%= publicActivity.getPlusObject().getContent() %>
            </dd>
        </dl>

    </section>
</div>
<div class="box">
    <section class="me">
        <%
            //fetch your profile object
            Person profile = plus.people().get("me").execute();

            //render it
        %>
        <h3>Your Profile</h3>
        <dl>
            <dt>Image</dt>
            <dd><a href="<%= profile.getUrl() %>"><img width="100"
                                                       src="<%= profile.getImage().getUrl() %>"/></a>
            </dd>
            <dt>ID</dt>
            <dd><%= profile.getId() %>
            </dd>
            <dt>Name</dt>
            <dd>
                <a href="<%= profile.getUrl() %>"><%= profile.getDisplayName() %>
                </a>
            </dd>
        </dl>
    </section>
</div>
<div class="largerbox">
    <section class="activity_list">
        <h3>Your Public Activity</h3>
        <%
            Plus.Activities.List listActivities = plus.activities().list("me", "public");
            listActivities.setMaxResults(5L);

            ActivityFeed activityFeed = listActivities.execute();
            List<Activity> activities = activityFeed.getItems();

            listActivities.setPageToken(activityFeed.getNextPageToken());
        %>
        <table>
          <thead>
          <tr>
            <th>ID</th>
            <th>Content</th>
          </tr>
          </thead>
          <tbody>
            <%
                while (activities != null) {
                    if (activityFeed.getItems() != null) {
                        for (Activity activity : activities) {
            %>
            <tr><td><a href="<%= activity.getUrl() %>"><%= activity.getId() %></a></td>
            <td><%= activity.getPlusObject().getContent() %></td></tr>
            <%
                        }
                        // We will know we are on the last page when the next page token is null.
                        // If this is the case, break.
                        if (activityFeed.getNextPageToken() == null) {
                            break;
                        }
                        // Prepare the next page of results
                        listActivities.setPageToken(activityFeed.getNextPageToken());
                        // Execute and process the next page request
                        activityFeed = listActivities.execute();
                        activities = activityFeed.getItems();
                    }
                }
              }
            %>
        </tbody>
      </table>
    </section>
</div>
</body>
</html>